package com.clothe.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.clothe.dao.IAdminInfoDAO;
import com.clothe.dto.AdminInfo;

public class AdminInfoDAOImpl implements IAdminInfoDAO {
	private Connection conn = null;

	public void setConn(Connection conn) {
		this.conn = conn;

	}

	@Override
	public boolean save(AdminInfo adminInfo) throws Exception {
		PreparedStatement ps = null;
		boolean flag = false;
		try {
			String sql = "insert into admin(admin_account,admin_password,admin_name) values(?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, adminInfo.getAdminAccount());
			ps.setString(2, adminInfo.getAdminPassword());
			ps.setString(3, adminInfo.getAdminName());
			if (ps.executeUpdate() > 0)
				flag = true;

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			ps.close();
		}
		return flag;
	}

	@Override
	public boolean delete(int id) throws Exception {
		PreparedStatement ps = null;
		boolean flag = false;
		try {
			String sql = "delete from  admin where id = ?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			if (ps.executeUpdate() > 0)
				flag = true;

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			ps.close();
		}
		return flag;
	}

	@Override
	public boolean update(AdminInfo adminInfo) throws Exception {
		PreparedStatement ps = null;
		boolean flag = false;
		try {
			String sql = "update admin  set admin_name where id=?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, adminInfo.getAdminName());
			ps.setInt(2, adminInfo.getId());
			if (ps.executeUpdate() > 0)
				flag = true;

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			ps.close();
		}
		return flag;
	}

	// @Override
	// public boolean updatePsw(AdminInfo adminInfo) throws Exception {
	// PreparedStatement ps = null;
	// boolean flag = false;
	// try {
	// String sql = "update  admin  set admin_password where id=?)";
	// ps = conn.prepareStatement(sql);
	// ps.setString(1, adminInfo.getAdminPassword());
	// ps.setInt(2, adminInfo.getId());
	// if (ps.executeUpdate() > 0)
	// flag = true;
	//
	// } catch (Exception e) {
	// e.printStackTrace();
	// throw e;
	// } finally {
	// ps.close();
	// }
	// return flag;
	// }

	@Override
	public AdminInfo get(int id) throws Exception {
		PreparedStatement ps = null;
		ResultSet rs = null;
		AdminInfo adminInfo = null;

		try {
			String sql = "select * from admin where id=?)";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				adminInfo = new AdminInfo();
				adminInfo.setAdminAccount(rs.getString("admin_account"));
				adminInfo.setAdminName(rs.getString("admin_name"));
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			rs.close();
			ps.close();
		}
		return adminInfo;
	}

	@Override
	public List<AdminInfo> list() throws Exception {
		PreparedStatement ps = null;
		ResultSet rs = null;
		AdminInfo adminInfo = null;
		List<AdminInfo> adminInfoList = null;
		try {
			adminInfoList = new ArrayList<AdminInfo>();
			String sql = "select * from user";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				adminInfo = new AdminInfo();
				adminInfo.setId(rs.getInt("id"));
				adminInfo.setAdminAccount(rs.getString("admin_account"));
				adminInfo.setAdminName(rs.getString("admin_name"));
				adminInfoList.add(adminInfo);
			}

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			rs.close();
			ps.close();
		}
		return adminInfoList;
	}

	@Override
	public AdminInfo login(String account, String password) throws Exception {
		PreparedStatement ps = null;
		ResultSet rs = null;
		AdminInfo admininfo = null;
		try {
			String sql = "select * from admin where admin_account = ? and admin_password = ?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, account);
			ps.setString(2, password);
			rs = ps.executeQuery();
			if (rs.next()) {
				admininfo = new AdminInfo();
				admininfo.setId(rs.getInt("id"));
				admininfo.setAdminName(rs.getString("admin_name"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			rs.close();
			ps.close();

		}
		return admininfo;
	}

}
